package top.broncho.lottery.util

import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info
import top.broncho.lottery.db.LotteryEntity
import top.broncho.lottery.model.lottery.Lottery
import top.broncho.lottery.model.ocr.OcrResult
import top.broncho.lottery.model.ocr.Prize
import top.broncho.lottery.ui.main.MainViewModule

fun LotteryEntity.checkPrize(lottery: Lottery): List<Prize> {
    val prizeList = ArrayList<Prize>()
    list.forEach {
        val count = it.last().toInt()//注数
        val currentBalls = it.subList(1, it.lastIndex)//需要对比是否中奖的7个球
        val targetBalls = lottery.lottery_res.split(",")//当期出奖的7个球
        val blueHit = currentBalls.last() == targetBalls.last()//蓝球是否一样
        var redHitCount = 0
        for (i in 0 until (targetBalls.size - 1)) {
            for (j in 0 until (currentBalls.size - 1))
                if (targetBalls[i] == currentBalls[j]) {
                    redHitCount++
                }
        }
        AnkoLogger<MainViewModule>().info {
            "checkPrize: currentBalls=$currentBalls," +
                    " targetBalls=$targetBalls, blueHit=$blueHit, redHitCount=$redHitCount"
        }
        if (blueHit && redHitCount < 3) {//六等奖 5元
            prizeList.add(Prize("六等奖", count, 5, currentBalls))
        } else if ((blueHit && redHitCount == 3) || redHitCount == 4) {//五等奖 10元
            prizeList.add(Prize("五等奖", count, 10, currentBalls))
        } else if ((blueHit && redHitCount == 4) || redHitCount == 5) {//四等奖 200元
            prizeList.add(Prize("四等奖", count, 200, currentBalls))
        } else if (blueHit && redHitCount == 5) {//三等奖 浮动
            val amount = lottery.lottery_prize[2].prize_amount
            prizeList.add(Prize("三等奖", count, amount.toLong(), currentBalls))
        } else if (redHitCount == 6) {//二等奖 浮动
            val amount = lottery.lottery_prize[1].prize_amount
            prizeList.add(Prize("二等奖", count, amount.toLong(), currentBalls))
        } else if (blueHit && redHitCount == 6) {//一等奖 浮动
            val amount = lottery.lottery_prize[2].prize_amount
            prizeList.add(Prize("一等奖", count, amount.toLong(), currentBalls))
        }
    }
    return prizeList
}